package com.dao;



import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;


import com.base.dao.AbsTaskBaseDao;

@SuppressWarnings("unchecked")
public class UserDao extends AbsTaskBaseDao{

	
	static Logger log = Logger.getLogger(UserDao.class);
	/**
	 * 用户注册信息
	 * @param userName
	 * @param password
	 * @return
	 */
	public boolean userRegiste(String username,String password){
		String sql = "insert into user (name,password) values ('"+username+"',AES_ENCRYPT('"+password+"','key'))";
		Session session = null;
		try {
			session = this.getSession();
			Query query = session.createSQLQuery(sql);
			query.executeUpdate();
			return true;
		} catch (Exception e ) {
			log.error("userRegiste"+e);
			return false;
		} finally {
			this.releaseSession(session);
		}
	}
	
	public boolean userLogin(String username,String password){
		String sql = "select * from user where name='"+username+"' and password = AES_ENCRYPT('"+password+"','key')";
		Session session = null;
		try {
			session = this.getSession();
			Query query = session.createSQLQuery(sql);
			int count = query.list().size();
			if (count == 1) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e ) {
			log.error("userLogin"+e);
			return false;
		} finally {
			this.releaseSession(session);
		}
	}

}
